package com.ruoyi.car.service.login;

import com.google.common.collect.Sets;
import com.ruoyi.car.domain.WxUser;
import com.ruoyi.car.service.IWxUserService;
import com.ruoyi.common.core.domain.entity.WxUserBo;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.SysPermissionService;
import lombok.SneakyThrows;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class WxUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private IWxUserService wxUserService;

    @SneakyThrows
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        WxUser wxUser = wxUserService.selectWxUserByPhone(username);
        if (StringUtils.isNull(wxUser)) {
            throw new Exception("登录用户不存在");
        }
        return createLoginUser(wxUser);
    }

    public UserDetails createLoginUser(WxUser wxUser) {
        WxUserBo wxUserBo = new WxUserBo();
        wxUserBo.setUserName(wxUser.getPhone());
        wxUserBo.setPassword(wxUser.getPassword());
        wxUserBo.setNickName(wxUser.getNickName());
        wxUserBo.setAvatarUrl(wxUser.getAvatarUrl());
        wxUserBo.setWxUserId(wxUser.getWxUserId());
        return new LoginUser(wxUserBo, Sets.newHashSet());
    }
}
